﻿using ITXHWEB.Models;
using ReflectionStudio.Core.Events;
using System;
using System.Data;
using System.Web.Mvc;
using WPFExcelReport;
using WPFExcelReport.Helpers;
using ITXHWEB.Infrastructure;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
namespace ITXHWEB.Controllers
{
    public partial class WBBSTController : ITXController
    { 
        [ChildActionOnly]
        public ActionResult SSHGBN(string returnUrl)
        {
            //插入到一个数据库表，然后批量进行移动记录，记录下来时间戳和申请的ID号码

            ModelState.Clear();
            MABF.CWBB.SLO = "01";
            MABF.CWBB.SLB = "CW";
            MABF.CWBB.LO = "01";
            MABF.CWBB.LB = "KO";
            MABF.CWBB.LP = "        ";
            if (WBBS == null)
            {
                TWBS = 0;
                HWBS = 0;
                GBN2SH();

            }
            return PartialView(MABF.CWBB);
        }
        public void GBN2SH()
        {
            TargetMTR = 0;
            MessageInfo msg = new MessageInfo();
            if (MABF.CVVT != null)
            {
                msg.LIEFSCH = MABF.CVVT.LIEFSCH;
                msg.AUFTYP = MABF.CVVT.AUFTYP;
                msg.KRS = MABF.CVVT.KRS;
                msg.LFD = MABF.CVVT.LFD;
                msg.POS = MABF.CVVT.POS;
                msg.LO = MABF.CWBB.SLO;
                msg.LB = MABF.CWBB.SLB;
                DataSet SResult = VHP.VWBB.WB2WH(msg);
                if (WBBS == null)
                {
                    WBBS = new List<WBBSTModel>();
                }
                WBBS.Clear();
                WBLIST = SResult.Tables[0].Copy();
                foreach (DataRow row in WBLIST.Rows)
                {
                    WBBSTModel m = new WBBSTModel();
                    m.WBBSTKNR = row["WBBSTKNR"].ToString();
                    m.LO = row["WBBLO"].ToString();
                    m.LB = row["WBBLB"].ToString();
                    m.LP = row["WBBLP"].ToString();
                    m.WHP = m.LO + m.LB + m.LP;
                    m.QMTR = Convert.ToDouble(row["WBBISTKLG"].ToString()) / 1000;
                    m.QYD = Helper.RoundX(m.QMTR / 0.9144, 2);
                    m.REMARK = "";
                    m.PACKNR = row["WBBPACKNR"].ToString();
                    WBBS.Add(m);
                    CPWBS += m.PACKNR + ",";
                }
                TWBS = WBBS.Count;
            }
        }
        [HttpPost]
        [AllowAnonymous]
        [ValidateAntiForgeryToken]
        public ActionResult SSHGBN(WBBSTModel model, string returnUrl)
        {
            if(MABF.CVVT.CONTROL == "OK ")
            {
                WZ04GBN(model);//发货操做 
                ModelState.Clear();
            }
            INFO = "发货尚未得到审批"; 
            return RedirectToAction(MAPP, MAPP);
        } 
        public void WZ04GBN(WBBSTModel model)
        {
            try
            {
                if (model == null)
                    return;
                INFO = "";
                dwData = VHP.VWBB.TBMain.NewRow();
                dwData["WBBMANDANT"] = app.Mandant;
                dwData["WBBGEBNR"] = model.GEBNR.PadRight(8);
                dwData["WBBAUFLFD"] = " AND WBBAUFKRS||WBBAUFLFD||WBBAUFPOS||WBBURLIEFSCH='" + MABF.CVVT.LIEFSCH+ MABF.CVVT.VKPAUF+"'";
                SResult = VHP.VWBB.QWBGEBNR(dwData);
                foreach (DataRow _dwWB in SResult.Tables[0].Rows)
                {
                    string art = _dwWB["WBBART"].ToString() + _dwWB["WBBFARBE"].ToString();
                    if(MABF.CVVT.VFART!=art)
                    {
                        INFO += _dwWB["WBBSTKNR"].ToString() + "不在范围内;";
                        continue;
                    }
                    if (MABF.CVVT.LIEFSCH != _dwWB["WBBURLIEFSCH"].ToString())
                    {
                        INFO += _dwWB["WBBSTKNR"].ToString() + "不在范围内;";
                        continue;
                    }
                    if (MABF.CVVT.VKPAUF != _dwWB["WBBAUFKRS"].ToString()+_dwWB["WBBAUFLFD"].ToString() + _dwWB["WBBAUFPOS"].ToString())
                    {
                        INFO += _dwWB["WBBSTKNR"].ToString() + "不在范围内;";
                        continue;
                    }
                    if (_dwWB["WBBLP"].ToString()=="KO")
                    {
                        INFO += _dwWB["WBBSTKNR"].ToString() + "已经出货;";
                        continue;
                    }
                    _dwWB["WBBAUFTYP"] = "K";
                    _dwWB["WBBAUFKRS"] = MABF.CVVT.KRS;
                    _dwWB["WBBAUFLFD"] = MABF.CVVT.LFD;
                    _dwWB["WBBAUFPOS"] = MABF.CVVT.POS;
                    _dwWB["WBBFERTKZ"] = "E";
                    _dwWB["WBBURLIEFSCH"] = MABF.CVVT.LIEFSCH;
                    _dwWB["WBBALIEFSCH"] = MABF.CVVT.LIEFSCH;
                    _dwWB["WBBAENDBEN"] = MABF.NEWUSER;
                    _dwWB["WBBAENDDAT"] = VHP.VWBB.DBDATE;
                    _dwWB["WBBGEBNR"] = "".PadRight(8); 
                    VHP.VWBB.MOVE2KO(_dwWB, MABF.CWBB.LO, MABF.CWBB.LB, MABF.CWBB.LP, RUNID);
                    HWBS++;
                }
                VHP.VWBW.POSTCHG();
                PM["AUFTYP"] = "K";
                PM["KRS"] = MABF.CVVT.KRS;
                PM["LFD"] = MABF.CVVT.LFD;
                PM["POS"] = MABF.CVVT.POS;
                PM["VKPAUF"] = MABF.CVVT.KRS + MABF.CVVT.LFD + MABF.CVVT.POS;
                PM["ORDERNUM"] = "K" + MABF.CVVT.KRS + MABF.CVVT.LFD + MABF.CVVT.POS;
                PM["LIEFSCH"] = MABF.CVVT.LIEFSCH;
                VHP.VVT.VVTCHG(PM);
                VHP.VVVL.VVCHG(PM);
            }
            catch(Exception ex)
            {
                INFO = ex.Message;
                Tracer.Error(ex.StackTrace, ex);
            }
            
        }
    }
}
